﻿using LineStickerDwonload.Helper;
using System;
using System.Collections.Generic;
using System.Net;
using System.ComponentModel;
using static LineStickerDwonload.Linehelper.Scanner;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency;
using CatUtilsDll.Utils.Currency.WebUtils;
using CatUtils.Utils.Helper;

namespace LineStickerDwonload.Linehelper
{
    internal class DownLoad
    {
        private static string LocalFloderPath = "";
        private static List<LineTool.DownloadInfo> AllFileLink = new List<LineTool.DownloadInfo> { };
        private static string LocalFileUrl = "";
        private static string LocalFilePath = "";

        /// <summary>
        /// 提示
        /// </summary>
        public static string ApngGifTip = "动态表情 Apng为火狐专属表情格式";

        public static string ApngToGifTip = "Apng转GIF动态图";

        private static void callBackDownStringCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Console.WriteLine(e.Error.Source);
                Console.WriteLine(e.Error.Message);
                if (LocalFileUrl.IndexOf("sticker_animation") >= 0
                   )
                {
                    Console.WriteLine("无动态表情，跳过。");
                    FileHelper.FileDelete(LocalFilePath);
                }

                PrintLog.Log("下载出错的链接：" + LocalFileUrl);
            }

            if (++LineStaticValue.CurrentProgress >= LineStaticValue.MaxProgress)
            {
                new ImgHelper().BackGroundToWrite(FloderNameHelper.GetFullFileNameList(LocalFloderPath, "*.png"));
                //FileNameHelper.CreateFileNameWithChlidFloder(FilePath,StaticValue.ImgClearTip)
                foreach (var item in FloderNameHelper.GetFullFileNameList(LocalFloderPath, "*.apng"))
                {
                    string ApngFilePath = FileNameHelper.CreateFileNameWithChildFloder(item, ApngGifTip);
                    string GifFilePath = FileNameHelper.CreateFileNameWithExtension(FileNameHelper.CreateFileNameWithChildFloder(item, ApngToGifTip), ".gif");
                    FileHelper.FileMove(item, ApngFilePath, true);
                    ApngToGif.Convert(item, GifFilePath);
                }
            }
            else
                Start();
        }

        private static void CallProgressCount(object sender, DownloadProgressChangedEventArgs e)
        {
        }

        internal static void InitStart(List<LineTool.DownloadInfo> ImgList, List<LineTool.DownloadInfo> SoundList, string FloderPath)
        {
            LocalFloderPath = FloderPath;

            AllFileLink.AddRange(ImgList);
            AllFileLink.AddRange(SoundList);

            FloderHelper.FloderExits(FloderPath, true);
            Start();
        }

        internal static void Start()
        {
            if (AllFileLink.Count <= 0)
                return;

            LineTool.DownloadInfo DownloadUrl = AllFileLink[0];
            LocalFileUrl = DownloadUrl.FileLink;
            LocalFilePath = LocalFloderPath + DownloadUrl.FileName;
            Console.WriteLine("Link" + LocalFileUrl);
            Console.WriteLine("待下载:" + AllFileLink.Count);
            if (LocalFileUrl == "")
            {
                Console.WriteLine("空连接");
                Start();
                return;
            }

            new WebHelper().DownloadToFileAsync(LocalFileUrl, LocalFilePath, callBackDownStringCompleted, CallProgressCount);
            AllFileLink.RemoveAt(0);
        }
    }
}